package com.rhtchr.angelbaby.customer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.rhtchr.angelbaby.domain.CustomerInfo;
import com.rhtchr.util.DefaultAction;
import com.rhtchr.util.domain.DictionaryItem;

/**
 * 客户注册功能。
 * 
 * @author limingju
 * 
 */
public class CustomerRegister extends DefaultAction {

	private CustomerDao customerDao;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final String VALIDATE_IMAGE_CODE = "validate_image_code";
	private final String INDEX = "index";
	private List<DictionaryItem> motherList = new ArrayList<DictionaryItem>();

	private CustomerInfo customer;

	private String validateImageCode;

	public CustomerRegister() {
		super();
	}

	public String input() {
		return ActionSupport.INPUT;
	}

	public String index() {
		return INDEX;
	}

	/**
	 * 把用户的注册信息写入到数据库中去。
	 * 
	 * @return 保存是否成功,根据保存结果转到不同的页面。
	 */
	public String save() {
		clearFieldErrors();
		boolean passwordEquals = (StringUtils.isNotEmpty(customer.getPassword()) && StringUtils.isNotEmpty(customer.getRepassWord()) && customer
			.getPassword().equals(customer.getRepassWord()));
		if (!passwordEquals) {
			addFieldError("customer.repassWord", "密码不能为空，并且，两次输入的密码必须一致！");
			return ActionSupport.INPUT;
		}

		String hql = "From CustomerInfo ci Where ci.userName = ? Or ci.mobileNumber = ? Or ci.email = ?";
		List<CustomerInfo> tmp = customerDao.find(hql, customer.getUserName(), customer.getMobileNumber(), customer.getEmail());
		if (tmp.size() > 0) {
			addFieldError("customer.userName", "用户名称、或手机号码、或邮件地址已经被注册使用过！请重新输入！");
			addFieldError("customer.mobileNumber", "用户名称、或手机号码、或邮件地址已经被注册使用过！请重新输入！");
			addFieldError("customer.email", "用户名称、或手机号码、或邮件地址已经被注册使用过！请重新输入！");
			return ActionSupport.INPUT;
		}
		Map<String, Object> session = ActionContext.getContext().getSession();
		String tmpCode = (String) session.get(VALIDATE_IMAGE_CODE);
		if (!(StringUtils.isNotEmpty(tmpCode) && tmpCode.equals(validateImageCode))) {
			setValidateImageCode("");
			addFieldError("validateImageCode", "验证码输入错误！请重新输入！");
			return ActionSupport.INPUT;
		}
		Integer id = customerDao.save(customer);
		if (null != id) {
			return ActionSupport.SUCCESS;
		}
		return ActionSupport.ERROR;
	}

	/**
	 * 
	 * @return
	 */
	public String customerRegister() {
		boolean passwordEquals = (StringUtils.isNotEmpty(customer.getPassword()) && StringUtils.isNotEmpty(customer.getRepassWord()) && customer
			.getPassword().equals(customer.getRepassWord()));
		if (!passwordEquals) {
			addFieldError("customer.repassWord", "密码不能为空，并且，两次输入的密码必须一致！");
			return ActionSupport.INPUT;
		}

		return ActionSupport.SUCCESS;
	}

	public List<DictionaryItem> getBabyStatusList() {
		return customerDao.getBabyStatusList();
	}

	public Map<Integer, List<DictionaryItem>> getCityMap() {
		return customerDao.getCityMap();
	}

	public CustomerInfo getCustomer() {
		if (null == customer) {
			customer = new CustomerInfo();
		}
		return customer;
	}

	public CustomerDao getCustomerDao() {
		return customerDao;
	}

	public List<DictionaryItem> getEducationLevelList() {
		return customerDao.getEducationLevelList();
	}

	public List<DictionaryItem> getFamilyIncomeLevelList() {
		return customerDao.getFamilyIncomeLevelList();
	}

	public List<DictionaryItem> getGenderList() {
		return customerDao.getGenderList();
	}

	public List<DictionaryItem> getMotherList() {
		if (motherList.size() > 0) {
			return motherList;
		}
		DictionaryItem mother = new DictionaryItem();
		mother.setCode(2); // gender 2: 女
		mother.setName("我是妈妈");
		DictionaryItem father = new DictionaryItem();
		father.setCode(1);// gender 1: 男
		father.setName("我是爸爸");
		//
		motherList.add(mother);
		motherList.add(father);
		return motherList;
	}

	public List<DictionaryItem> getProvinceList() {
		return customerDao.getProvinceList();
	}

	public void setCustomer(CustomerInfo customerInfo) {
		this.customer = customerInfo;
	}

	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	public String getValidateImageCode() {
		return validateImageCode;
	}

	public void setValidateImageCode(String validateImageCode) {
		this.validateImageCode = validateImageCode;
	}

}
